package cn.shuangliang.contract.service;

import cn.shuangliang.contract.dao.ContracthistoryMapper;
import cn.shuangliang.contract.dao.ContractitemMapper;
import cn.shuangliang.contract.model.Contracthistory;
import cn.shuangliang.contract.model.Contractitem;
import cn.shuangliang.contract.util.LogUtil;
import cn.shuangliang.contract.vo.Constant;
import cn.shuangliang.contract.dao.ContractMapper;
import cn.shuangliang.contract.model.Contract;
import cn.shuangliang.contract.vo.Page;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
@Service
//@Scope("prototype")
@Transactional(readOnly = true,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
public class ContractService {

    @Resource
    private ContractMapper mapper;
    @Resource
    private ContractitemMapper m2;
    @Resource
    private ContracthistoryMapper hmaper;
    public ContractService(){
       // System.out.println("ContractService被new 了");
    }
    public Page<Contract> search(int pageNo, String no, String type, Date start, Date end) {
        List<Contract> search = mapper.search(pageNo, Constant.pageSize, no, type, start, end);
        int total = mapper.searchTotal(no, type, start, end);
        return new Page<Contract>(search, pageNo, total, Constant.pageSize);

    }

    //增加,合同，和对应的合同项
    @Transactional(readOnly = false)
    public int add(Contract cr, List<Contractitem> list) {
        mapper.insertSelective(cr);
        for (Contractitem ci : list) {
            ci.setContrid(cr.getId());
            m2.insert(ci);
        }
        return 1;
    }
    //增加合同
    @Transactional(readOnly = false)
    public int add(Contract cr) {
        return mapper.insertSelective(cr);
    }
    //根据是否有id,决定是更新还是增加
    public int add(Contractitem contractitem) {

        if (contractitem.getId() != null && contractitem.getId() > 0) {
            return m2.updateByPrimaryKey(contractitem);
        }
        return m2.insert(contractitem);


    }

    //删除合同及其子项
    @Transactional(readOnly = false)
    public int del(long id) {
        return mapper.delContract(id);

    }

    //查看全部的关联信息
    public Contract find(Long id) {

        return mapper.find(id);


    }

    public Contract findWithHis(Long id) {

        return mapper.findWithHis(id);

    }

    public int confirm(Long id, boolean b) {

        return mapper.confirm(id, b);

    }
    @Transactional(readOnly = false)
    public int addHis(Contracthistory c) {

        int r = hmaper.insert(c);
        int r1 = mapper.updateAt(c.getCid(), c.getProdid(), c.getAtweight(), c.getAtnum());

        return r * r1;

    }

    public int update(Contract cr) {
        return mapper.updateByPrimaryKeySelective(cr);
    }

    public int delCItem(Long id, Long cid) {
        int historys = mapper.checkHistorys(id, cid);
        if (historys < 1) {
            return hmaper.deleteByPrimaryKey(id);
        }
        return -1;//有记录

    }
}
